<template>
	<el-card class="layout-query">
		<el-form ref="queryRef" :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
			<el-form-item prop="name">
				<el-input v-model="state.queryForm.name" placeholder="群名"></el-input>
			</el-form-item>
			<el-form-item prop="ownerIds">
				<im-user-select v-model="state.queryForm.ownerIds" :placeholder="'群主'" :multiple="true"></im-user-select>
			</el-form-item>
			<el-form-item prop="createdTime">
				<el-date-picker v-model="state.queryForm.createdTime" type="daterange" range-separator="-"
					start-placeholder="创建时间从" end-placeholder="至" value-format="YYYY-MM-DD HH:mm:ss" />
			</el-form-item>
			<el-form-item>
				<el-button icon="Search" type="primary" @click="getDataList()">查询</el-button>
			</el-form-item>
			<el-form-item>
				<el-button icon="RefreshRight" @click="reset(queryRef)">重置</el-button>
			</el-form-item>
		</el-form>
	</el-card>

	<el-card>
		<el-table v-loading="state.dataListLoading" :data="state.dataList" border class="layout-table">
			<el-table-column prop="headImageThumb" label="群头像" header-align="center" align="center">
				<template #default="scope">
					<el-avatar shape="square" :size="35" :src="scope.row.headImageThumb"></el-avatar>
				</template>
			</el-table-column>
			<el-table-column prop="name" label="群名" header-align="center" align="center"></el-table-column>
			<el-table-column prop="ownerUserName" label="群主" header-align="center" align="center"></el-table-column>
			<el-table-column prop="memberCount" label="成员数量" header-align="center" align="center"></el-table-column>
			<el-table-column prop="createdTime" label="创建时间" header-align="center" align="center"></el-table-column>
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="250">
				<template #default="scope">
					<el-button v-auth="'im:group:info'" type="primary" link
						@click="detailHandle(scope.row.id)">详情</el-button>
					<el-button v-auth="'im:group:ban'" v-show="!scope.row.isBanned" type="danger" link
						@click="banHandle(scope.row)">封禁</el-button>
					<el-button v-auth="'im:group:ban'" v-show="scope.row.isBanned" type="primary" link
						@click="unbanHandle(scope.row)">解封</el-button>
					<el-button v-auth="'im:group:member'" type="primary" link
						@click="showMemberHandle(scope.row.id)">查看成员</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination :current-page="state.page" :page-sizes="state.pageSizes" :page-size="state.limit"
			:total="state.total" layout="total, sizes, prev, pager, next, jumper" @size-change="sizeChangeHandle"
			@current-change="currentChangeHandle">
		</el-pagination>
		<!-- 详情 -->
		<detail ref="detailRef" @refreshDataList="getDataList"></detail>
		<el-drawer v-model="memberVisible" title="成员列表" :size="900" :close-on-press-escape="false">
			<member ref="memberRef"></member>
		</el-drawer>
	</el-card>
</template>

<script setup lang="ts" name="ImGroupIndex">
import { useCrud } from '@/hooks'
import { nextTick, reactive, ref } from 'vue'
import { IHooksOptions } from '@/hooks/interface'
import { useGroupBanApi, useGroupUnbanApi } from '@/api/im/group'
import { ElMessage, ElMessageBox } from 'element-plus'
import Detail from './detail.vue'
import Member from './member.vue'

const state: IHooksOptions = reactive({
	dataListUrl: '/im/group/page',
	queryForm: {
		name: '',
		ownerIds: [],
		createdTime: ''
	}
})
const memberVisible = ref(false);
const queryRef = ref()
const detailRef = ref()
const memberRef = ref()

const detailHandle = (id?: number) => {
	detailRef.value.init(id)
}
const banHandle = (group: any) => {
	ElMessageBox.prompt('封禁原因:', '确定对该群组进行封禁？', {
		inputPattern: /.+/,
		inputErrorMessage: '请输入封禁原因',
		confirmButtonText: '确定',
		cancelButtonText: '取消'
	}).then(({ value }) => {
		const data = { id: group.id, reason: value }
		useGroupBanApi(data).then(() => {
			group.isBanned = true;
			ElMessage.success(`群组'${group.name}'已被封禁`);
		})
	})
}

const unbanHandle = (group: any) => {
	ElMessageBox.confirm('确定解除该群组的封禁状态？', '提示', {
		confirmButtonText: '确定',
		cancelButtonText: '取消'
	}).then(() => {
		const data = { id: group.id }
		useGroupUnbanApi(data).then(() => {
			group.isBanned = false;
			ElMessage.success(`群组'${group.name}'解封成功`);
		})
	})
}

const showMemberHandle = (id: number) => {
	memberVisible.value = true;
	nextTick(() => memberRef.value.init(id));
}

const { getDataList, sizeChangeHandle, currentChangeHandle, reset } = useCrud(state)
</script>
